<?php
class lib_db_factory
{
    protected static $instance;
    private $driver = 'mysql';
    public static function get_instance ($type = 'default')
    {
        if (! self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance->factory($type);
    }
    private function __construct ()
    {}
    private function __clone ()
    {}
    //----------------------------------------------------------
    public function factory ($type = 'default')
    {
        $config = $this->parse_config($type);
        if (isset($config['driver'])) {
            $driver = strtolower(trim($config['driver']));
            unset($config['driver']); //删除驱动元素
        } else {
            $driver = $this->driver;
        }
        $_classname = 'lib_db_' . $driver;
        if (class_exists($_classname)) {
            $db_driver = new $_classname($config);
            if (method_exists($db_driver, 'connect')) {
                $db_driver->connect();
            }
            return $db_driver;
        }
    }
    //----------------------------------------------------------
    private function parse_config ($type)
    {
        $config = configs::reloadCof();
        //$config=R::getClass('config');
        //debug($config,$configs);
        return $config->db[$config->db_types];
    }
}